package biz

import (
	"New-lesson-part2/custom_error"
	"New-lesson-part2/internal"
	"New-lesson-part2/model"
	"New-lesson-part2/proto/pb"
	"context"
	"errors"
	"github.com/golang/protobuf/ptypes/empty"
)

type CartOrderServer struct {
}

func (s CartOrderServer) ShopCartItemList(ctx context.Context, req *pb.AccountReq) (*pb.CartItemListRes, error) {
	var res pb.CartItemListRes
	var shopList []model.ShopCart
	if req.AccountId == 0 {
		return nil, errors.New(custom_error.ParameterError)
	}
	r := internal.DB.Where("account_id = ?", req.AccountId).Find(&shopList)
	if r.Error != nil {
		return nil, errors.New(custom_error.CartOrderSystemError)
	}
	if r.RowsAffected < 1 {
		return &res, nil
	}
	for _, i := range shopList {
		res.ItemList = append(res.ItemList, ConvertShopCartModel2Pb(i))
	}
	res.Total = int32(r.RowsAffected)
	return &res, nil
}

func (s CartOrderServer) AddShopCartItem(ctx context.Context, req *pb.ShopCartReq) (*pb.CartItemRes, error) {
	tx := internal.DB.Begin()
	defer tx.Commit()
	var res *pb.CartItemRes
	var shopModel model.ShopCart
	//如果购物车没有商品,那就添加到购物车中.
	//如果有商品那就更新数量
	r := tx.Where("account_id = ?", req.AccountId).Where("product_id = ?", req.ProductId).First(&shopModel)
	if r.RowsAffected < 1 {
		shopModel.ID = req.Id
		shopModel.Num = req.Num
		shopModel.ProductId = req.ProductId
		shopModel.AccountId = req.AccountId
		shopModel.Checked = req.Checked
	} else {
		shopModel.Num += req.Num
	}
	save := tx.Save(&shopModel)
	if save.Error != nil {
		tx.Rollback()
		return nil, errors.New(custom_error.CartOrderSystemError)
	}
	if save.RowsAffected < 1 {
		tx.Rollback()
		return nil, errors.New(custom_error.CreateShopCartFailed)
	}
	res = ConvertShopCartModel2Pb(shopModel)
	return res, nil
}

func (s CartOrderServer) UpdateShopCartItem(ctx context.Context, req *pb.ShopCartReq) (*empty.Empty, error) {
	tx := internal.DB.Begin()
	defer tx.Commit()
	var cart model.ShopCart
	r := tx.Where("account_id = ?", req.AccountId).Where("product_id = ?", req.ProductId).First(&cart)
	if r.RowsAffected < 1 {
		return nil, errors.New(custom_error.ShopCartNotFound)
	}
	if req.Num < 1 {
		return nil, errors.New(custom_error.ParameterError)
	}
	cart.Num = req.Num
	cart.Checked = req.Checked
	save := tx.Save(&cart)
	if save.RowsAffected < 1 {
		tx.Rollback()
		return nil, errors.New(custom_error.CreateShopCartFailed)
	}

	return &empty.Empty{}, nil
}

func (s CartOrderServer) DeleteShopCartItem(ctx context.Context, req *pb.DeleteShopCartReq) (*empty.Empty, error) {
	tx := internal.DB.Begin()
	defer tx.Commit()
	var cart model.ShopCart
	if req.AccountId == 0 || req.ProductId == 0 {
		return nil, errors.New(custom_error.ParameterError)
	}
	r := tx.Where(&model.ShopCart{AccountId: req.AccountId, ProductId: req.ProductId}).Delete(&cart)
	if r.RowsAffected < 1 {
		tx.Rollback()
		return nil, errors.New(custom_error.DeleteShopCartFailed)
	}
	return &empty.Empty{}, nil
}

func ConvertShopCartModel2Pb(cart model.ShopCart) *pb.CartItemRes {
	r := &pb.CartItemRes{
		Id:        cart.ID,
		AccountId: cart.AccountId,
		ProductId: cart.ProductId,
		Num:       cart.Num,
		Checked:   cart.Checked,
	}
	return r
}
